package com.yeyks.controller;

import com.yeyks.cncallback.CNBookingCallBackService;
import com.yeyks.common.base.Arguments;
import com.yeyks.common.utils.AesUtil;
import com.yeyks.common.utils.IPUtils;
import com.yeyks.common.utils.apputil.redis.RedisService;
import com.yeyks.common.utils.apputil.rediskey.RedisKey;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;

/**
 * 回调更新接口
 *
 * @author: dagny
 * @date: 2019/9/23 0023 9:55
 */
@Api(tags = "数据回调推送CbReturnController", description = "回调推送接口信息")
@RestController
@RequestMapping("/cb/return")
@Slf4j
public class CbReturnController {

    @Autowired
    private RedisService redisService;

    @Autowired
    private CNBookingCallBackService cbReturnCallbackService;

    @ApiOperation("回调接口")
    @PostMapping("/callback")
    public String cbReturnCallback(HttpServletRequest request) {

        String ip = IPUtils.getIpAddr(request);
        //log.info("请求的真实IP【{}】, 请求的request为{}",ip,request);
         //默认30000为成功
        String code = "30000";
        try {

            String body = initBytes(request);
            //String body = requestParamsToMap(request);

            if(Arguments.strIsNull(body)){
                log.info("接收的body数据为空");
                code = "-1";
            }
            //密文解析
            String xml = AesUtil.desEncrypt(body);
            if (Arguments.strNotNull(xml)) {
                //转换json数据
                cbReturnCallbackService.cbReturnCallback(xml);
            }
        } catch (Exception e) {
            //log.info("处理回调数据异常：{}",e.getMessage());
        }
        return code;
    }


    /**
     * 第一次执行要进行数据的初始化
     *
     * @throws IOException
     */
    private String initBytes(HttpServletRequest request) throws IOException {
        StringBuilder buffer = new StringBuilder();
        BufferedReader reader = request.getReader();
        String line;
        while ((line = reader.readLine()) != null) {
            buffer.append(line);
        }

        return buffer.toString();
    }

    @ApiOperation("查询房态回调记录")
    @PostMapping("/query/calendar")
    public String getByCalendarIdInRedis(@RequestParam(value = "calendarId") String calenderId){

        String json = redisService.get(RedisKey.CNBOOKING_CALENDAR_PRICE + ":" + calenderId);
        if(StringUtils.isEmpty(json)){
            return "没有这条数据";
        }
        return json;
    }

    @ApiOperation("查询房态[99999]Price回调记录")
    @PostMapping("/query/max/price/calendar")
    public String getMaxPriceByCalendarIdInRedis(@RequestParam(value = "calendarId") String calenderId){

        String json = redisService.get(RedisKey.CNBOOKING_MAX_PRICE + ":" + calenderId);

        if(StringUtils.isEmpty(json)){
            return "没有这条数据";
        }
        return json;
    }


}
